package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.Config;
import com.hyk.dto.R;
import com.hyk.service.ConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "后台参数配置")
@RequestMapping("/configs")
public class ConfigController {

    @Resource
    private ConfigService configService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping
    @ApiOperation("根据规则查询配置数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "规则类型"),
            @ApiImplicitParam(name = "code",value = "规则代码"),
            @ApiImplicitParam(name = "name",value = "规则名称"),
            @ApiImplicitParam(name = "current",value = "当前页数"),
            @ApiImplicitParam(name = "size",value = "每页大小")
    })
    @PreAuthorize("hasAuthority('config_query')")
    public R<Page<Config>> list(@ApiIgnore Page<Config> page,
                                String type,
                                String code,
                                @RequestParam(required = false) String name){
        QueryWrapper<Config> wrapper = new QueryWrapper<>();
        if (type != null && type != ""){
            wrapper.like("type",type);
        }
        if (code != null && code != ""){
            wrapper.like("code",code);
        }
        if (name != null && name != ""){
            wrapper.like("name",name);
        }
        wrapper.orderByDesc("created");
        return R.ok(configService.page(page,wrapper));
    }

    @PostMapping
    @ApiOperation("新建配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "规则类型"),
            @ApiImplicitParam(name = "code",value = "规则代码"),
            @ApiImplicitParam(name = "name",value = "规则名称"),
            @ApiImplicitParam(name = "desc",value = "规则描述"),
            @ApiImplicitParam(name = "value",value = "值")
    })
    @PreAuthorize("hasAuthority('config_create')")
    public R add(@RequestBody @Validated Config config){
        boolean save = configService.save(config);
        if (save) {
            List<Config> list = configService.list();
            for(Config configX : list){
                redisTemplate.opsForHash().put(configX.getType(),configX.getCode(),configX.getValue());
            }
            return R.ok("新建成功");
        }
        return R.fail("新建失败");
    }

    @PatchMapping
    @ApiOperation("编辑配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "type",value = "规则类型"),
            @ApiImplicitParam(name = "code",value = "规则代码"),
            @ApiImplicitParam(name = "name",value = "规则名称"),
            @ApiImplicitParam(name = "desc",value = "规则描述"),
            @ApiImplicitParam(name = "value",value = "值")
    })
    @PreAuthorize("hasAuthority('config_update')")
    public R update(@RequestBody @Validated Config config){
        boolean update = configService.updateById(config);
        if (update) {
            List<Config> list = configService.list();
            for(Config configX : list){
                redisTemplate.opsForHash().put(configX.getType(),configX.getCode(),configX.getValue());
            }
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

}
